package pers.qtong.tank;

import pers.qtong.tank.strategy.DefaultFireStrategy;
import pers.qtong.tank.strategy.FireStrategy;

import java.awt.*;
import java.io.Serializable;
import java.util.Random;

public class Tank extends GameObject implements Serializable {

    public int x;
    public int y;

    private int oldX;
    private int oldY;
    public Dir dir;
    private final static int SPEED = PropertiesMgr.getInstance().getInt("tankSpeed");
    public boolean moving = true;
    public boolean living = true;

    public Group group;

    private FireStrategy fireStrategy;

    public static int WIDTH = ResourceMgr.goodTankD.getWidth();
    public static int HEIGHT = ResourceMgr.goodTankD.getHeight();

    private Random random = new Random();

    public Rectangle rectangle = new Rectangle();

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public boolean isLiving() {
        return living;
    }

    public void setLiving(boolean living) {
        this.living = living;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public Tank(int x, int y, Dir dir, Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;

        rectangle.width = WIDTH;
        rectangle.height = HEIGHT;

        PropertiesMgr propertiesMgr = PropertiesMgr.getInstance();
        try{
            /*if(group == Group.BAD) {
                String info = propertiesMgr.getString("badFs");
                fireStrategy  = (DefaultFireStrategy)Class.forName(info).newInstance();
            }else {
                String info = propertiesMgr.getString("goodFs");
                fireStrategy = (FourFireStrategy) Class.forName(info).newInstance();
            }*/
            fireStrategy = (DefaultFireStrategy) Class.forName(propertiesMgr.getString("badFs")).newInstance();
        }catch (Exception e) {
            e.printStackTrace();
        }

        GameModel.getInstance().add(this);
//       fireStrategy = group == Group.BAD ? new DefaultFireStrategy() : new FourFireStrategy();
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    @Override
    public void paint(Graphics g) {
        if (!living) {
            GameModel.getInstance().remove(this);
            if(group == Group.GOOD) {
                new Thread(() -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    setLiving(true);
                    GameModel.getInstance().add(this);
                }).start();
            }
        }
        switch (dir) {
            case LEFT:
                g.drawImage(this.group == Group.BAD ? ResourceMgr.badTankL : ResourceMgr.goodTankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.BAD ? ResourceMgr.badTankR : ResourceMgr.goodTankR, x, y, null);
                break;
            case UP:
                g.drawImage(this.group == Group.BAD ? ResourceMgr.badTankU : ResourceMgr.goodTankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.BAD ? ResourceMgr.badTankD : ResourceMgr.goodTankD, x, y, null);
                break;
        }
        move();
    }

    private void randomDir() {
        dir = Dir.values()[random.nextInt(4)];
    }

    private void move() {
        oldX = x;
        oldY = y;
        if (isMoving()) {
            switch (dir) {
                case LEFT:
                    x -= SPEED;
                    break;
                case RIGHT:
                    x += SPEED;
                    break;
                case UP:
                    y -= SPEED;
                    break;
                case DOWN:
                    y += SPEED;
                    break;
            }
        }
        if(group == Group.BAD) {    //敌人坦克
            if (random.nextInt(100) > 90) {
                fire();
                randomDir();
            }
        }
        boundCheck();
        rectangle.x = this.x;
        rectangle.y = this.y;
    }

    //防止出边界处理
    private void boundCheck() {
        if(x < 2) x = 2;
        if(y < 2) y = 2;
        if(y > TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2) y = TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2;
        if(x > TankFrame.GAME_WIDTH - Tank.WIDTH - 2) x = TankFrame.GAME_WIDTH - Tank.WIDTH - 2;
    }

    public void fire() {
        fireStrategy.fire(this);
    }

    public void die() {
        living = false;
//        gm.explodes.add(new Explode(x, y, gm));
    }

    public void stop() {
        moving = false;
    }

    public void back() {
        x = oldX;
        y = oldY;
    }

}
